<template>
  <div class="common-layout linear-gradient">
    <el-container>
      <el-header class="header-container">
        <div class="headerTitle">查询条件</div>
        <div class="header-content">
          <el-form :model="headerSearchForm" label-width="auto" class="header-search-form">
            <el-form-item label="变量组ID">
              <el-input v-model="headerSearchForm.themeGroupId" placeholder="请输入变量组ID" />
            </el-form-item>
            <el-form-item label="变量名">
              <el-input v-model="headerSearchForm.themeVariable" placeholder="请输入变量名" />
            </el-form-item>
            <el-form-item label="描述">
              <el-input v-model="headerSearchForm.themeVariableDescription" placeholder="请输入描述" />
            </el-form-item>
          </el-form>
          <div class="header-buttons">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button type="primary" @click="handleReset">重置</el-button>
            <el-button type="primary" @click="handleAdd">添加</el-button>
          </div>
        </div>
      </el-header>
      <el-main>
        <div class="table-container-main">
          <el-table
            :data="tableData"
            :header-cell-style="{ background: '#eef1f6', color: '#606266', textAlign: 'center' }"
            :cell-style="{ textAlign: 'center' }"
          >
            <el-table-column prop="serialNumber" label="序号" fixed="left">
              <template #default="scope">
                <span>{{ scope.$index + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="themeGroupId" label="变量组ID" />
            <el-table-column prop="productionLine" label="产线" />
            <el-table-column prop="themeVariable" label="主题变量名" />
            <el-table-column prop="themeVariableDescription" label="描述" />
            <el-table-column label="操作" width="240" fixed="right">
              <template #default="scope">
                <el-button type="warning" @click="handleEdit(scope.row)">修改</el-button>
                <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination">
          <el-pagination
            v-model:current-page="pagination.currentPage"
            v-model:page-size="pagination.pageSize"
            :page-sizes="[10, 20, 30, 50]"
            :total="pagination.total"
            layout="total, sizes, prev, pager, next"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-main>
    </el-container>
  </div>

  <el-dialog v-model="dialogVisible" :title="dialogTitle" :before-close="handleClose" ref="dialogRef" @closed="cancel">
    <el-form :model="dialogForm" label-width="auto">
      <el-form-item label="变量组ID">
        <el-input v-model="dialogForm.themeGroupId" placeholder="请输入变量组ID" disabled />
      </el-form-item>
      <el-form-item label="产线">
        <el-input v-model="dialogForm.productionLine" placeholder="请输入变量名" />
      </el-form-item>
      <el-form-item label="变量名">
        <el-input v-model="dialogForm.themeVariable" placeholder="请输入变量名" disabled />
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="dialogForm.themeVariableDescription" placeholder="请输入描述" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="dialogFormSubmit"> 提交 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts">
  import { ref, reactive, onMounted, onUnmounted, nextTick, defineComponent } from 'vue';
  import { ElMessage, ElMessageBox } from 'element-plus';
  import './steelCoilServiceModelCommonStyle.less';
  import { api } from '/@/api/steelCoilServiceModel/subjectData';

  interface SearchForm {
    themeGroupId: string;
    themeVariable: string;
    themeVariableDescription: string;
  }

  interface dialogForm {
    themeGroupId: string; // 主题组id
    themeVariable: string; // 主题变量名=数据字典的变量名
    productionLine: string; // 产线
    themeVariableDescription: string; // 主题变量描述
    toc: string; // 创建时间
    tom: string; // 修改时间
  }

  export default defineComponent({
    name: 'SubjectData',
    setup() {
      // 表单数据
      const headerSearchForm = reactive<SearchForm>({
        themeGroupId: '',
        themeVariable: '',
        themeVariableDescription: '',
      });

      // 表格数据
      const tableData = ref([{ themeGroupId: '', themeVariable: '', productionLine: '', themeVariableDescription: '', toc: '', tom: '' }]);

      let dialogVisible = ref(false);
      let dialogTitle = ref('');
      const dialogForm = reactive<dialogForm>({
        themeGroupId: '',
        themeVariable: '',
        productionLine: '',
        themeVariableDescription: '',
        toc: '',
        tom: '',
      });

      // 分页数据
      const pagination = reactive({
        currentPage: 1,
        pageSize: 10,
        total: tableData.value.length,
      });

      // 窗口大小变化处理
      const handleResize = () => {
        nextTick(() => {
          // 这里可以添加表格自适应高度的逻辑
        });
      };

      // 获取表格数据
      const fetchTableData = async () => {
        try {
          const param = {
            page: {
              pageIndex: pagination.currentPage,
              pageSize: pagination.pageSize,
            },
            dto: headerSearchForm,
          };
          const res = await api.page(param);
          tableData.value = res.data.data;
          pagination.currentPage = res.data.data.current;
          pagination.total = res.data.count;
        } catch (error) {
          ElMessage.error('获取数据失败');
        }
      };

      // 查询方法
      const handleSearch = () => {
        pagination.currentPage = 1;
        fetchTableData();
      };

      // 重置方法
      const handleReset = () => {
        (Object.keys(headerSearchForm) as Array<keyof SearchForm>).forEach((key) => {
          headerSearchForm[key] = '';
        });
        handleSearch();
      };

      // 添加方法
      const handleAdd = () => {
        // 这里添加新增逻辑
        console.log('添加');
        dialogVisible.value = true;
        dialogTitle.value = '新增';
      };

      // 编辑方法
      const handleEdit = (row: any) => {
        // 这里添加编辑逻辑
        dialogVisible.value = true;
        dialogTitle.value = '修改';
        Object.assign(dialogForm, { ...row });
      };

      const cancel = () => {
        dialogVisible.value = false;
        Object.assign(dialogForm, {
          themeGroupId: '',
          themeVariable: '',
          productionLine: '',
          themeVariableDescription: '',
          toc: '',
          tom: '',
        });
      };

      const dialogFormSubmit = async () => {
        try {
          if (dialogTitle.value === '修改') {
            await api.update(dialogForm);
          } else {
            await api.add(dialogForm);
          }
        } catch (error) {
          ElMessage.error('操作数据失败');
        } finally {
          ElMessage.success('操作成功');
          Object.assign(dialogForm, {
            themeGroupId: '',
            themeVariable: '',
            productionLine: '',
            themeVariableDescription: '',
            toc: '',
            tom: '',
          });
          dialogVisible.value = false;
          fetchTableData();
        }
      };

      // 删除方法
      const handleDelete = async (row: any) => {
        try {
          const params = {
            themeGroupId: row.themeGroupId,
            themeVariable: row.themeVariable,
          };
          await api.delete(params);
        } catch (error) {
          ElMessage.error('删除失败');
        } finally {
          fetchTableData();
        }
      };

      const handleClose = (done: () => void) => {
        ElMessageBox.confirm('Are you sure to close this dialog?')
          .then(() => {
            done();
          })
          .catch(() => {
            // catch error
          });
      };

      // 分页大小变化
      const handleSizeChange = (val: number) => {
        pagination.pageSize = val;
        fetchTableData();
      };

      // 页码变化
      const handleCurrentChange = (val: number) => {
        pagination.currentPage = val;
        fetchTableData();
      };

      // 生命周期钩子
      onMounted(() => {
        fetchTableData();
        window.addEventListener('resize', handleResize);
      });

      onUnmounted(() => {
        window.removeEventListener('resize', handleResize);
      });

      return {
        headerSearchForm,
        tableData,
        pagination,
        handleSearch,
        handleReset,
        handleAdd,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        dialogVisible,
        dialogTitle,
        handleClose,
        cancel,
        dialogForm,
        dialogFormSubmit,
      };
    },
  });
</script>
<!-- <style lang="less" scoped>
  @border-radius: 4px;
  @primary-color: #2c69e8;
  @box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  .common-layout {
    width: 100%;
    height: 100%;
    position: relative;
    background-color: #fff;

    &.linear-gradient {
      background-image: linear-gradient(to top, white, @primary-color) !important;
    }
  }
  .header-container {
    width: 98%;
    margin-left: 1%;
    margin-top: 4vh;
    margin-bottom: 2vh;
    border-radius: @border-radius;
    height: 11vh;
    background-color: #fff;
    box-shadow: @box-shadow;

    .headerTitle {
      width: 100%;
      height: 4vh;
      display: flex;
      align-items: center;
    }
    .header-content {
      width: 100%;
      height: 6vh;
      display: flex;
      justify-content: space-between;

      .header-search-form {
        width: 70%;
        height: 7vh;
        display: flex;
        align-items: center;
        gap: 20px;
        position: relative;
        z-index: 1;
      }

      .header-buttons {
        display: flex;
        align-items: center;
        gap: 10px;

        .el-button {
          min-width: 80px;
        }
      }
    }
  }

  .table-container-main {
    border-radius: @border-radius;
    height: 72vh;
    background-color: #fff;
    box-shadow: @box-shadow;
    display: flex;
    align-items: center;

    .el-table {
      width: calc(100% - 40px);
      height: calc(100% - 40px);
      margin-left: 20px;
    }
  }

  .pagination {
    margin-top: 2vh;
    display: flex;
    justify-content: flex-end;
    padding: 10px 0;
  }
</style> -->
